package cn.shengchao.dubbo.api.wecom;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.shengchao.examstar.rpc.wecom.api.ContactUserServiceApi;
import cn.shengchao.examstar.rpc.wecom.dto.WeComDeptUserDto;
import cn.shengchao.qywx.infrastrure.po.QywxUser;
import cn.shengchao.qywx.infrastrure.service.QywxUserService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.Date;
import java.util.List;

/**
 * @program: examstarapi
 * @description: 成员变更事件
 * @author: Xiong weiqiang
 * @create: 2025-04-16 21:21
 */
@DubboService
@RequiredArgsConstructor
public class ContactUserServiceDubboApi implements ContactUserServiceApi {
    private final QywxUserService qywxUserService;
    @Override
    public void saveUser(WeComDeptUserDto dto){
        List<QywxUser> qywxUsers=qywxUserService.list(Wrappers.lambdaQuery(QywxUser.class)
                .eq(QywxUser::getUserId,dto.getUserId())
                .eq(QywxUser::getStatus,1)
        );
        if (CollUtil.isEmpty(qywxUsers)){
            QywxUser qywxUser=new QywxUser();
            BeanUtil.copyProperties(dto,qywxUser);
            qywxUser.setAsFollower(1);
            qywxUser.setLoadStatus(0);
            qywxUser.setLastUpdateTime(new Date());
            qywxUserService.save(qywxUser);
        }
    }
    @Override
    public void updateUserNewId(String userId, String newUserId) {
        qywxUserService.update(Wrappers.lambdaUpdate(QywxUser.class).eq(QywxUser::getUserId,userId).eq(QywxUser::getStatus,1).set(QywxUser::getUserId,newUserId));
    }

    @Override
    public void deleteUser(String userId) {
        qywxUserService.update(Wrappers.lambdaUpdate(QywxUser.class).eq(QywxUser::getUserId,userId).set(QywxUser::getStatus,5));

    }
}